package com.situ.study.day5;

import org.junit.Test;

import java.util.Scanner;

public class ArrayDemo {
    @Test
    public void test1() {
        int score1 = 94;
        int score2 = 34;
        int score3 = 64;
        //int[] scoreArray = new int[3];
        int[] scores = new int[3];

        double[] array1 = new double[3];
        boolean[] array2 = new boolean[3];
        byte[] array3 = new byte[3];
        //存放Student类型的数组，这个数组里面可以放三个学生
        Student[] array4 = new Student[3];

        int[] array5 = {23, 34, 56, 7};
        System.out.println(array5.length);
        int[] array6 = new int[]{23, 34, 56, 7, 56};
    }

//    1、存放整数类型的数组
//    2、长度是4
//    3、只能存放int类型
    @Test
    public void test2() {
        //new出来对象
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        //数组(所有数据结构)最重要的操作就是遍历
        System.out.println("length: " + array.length);
        for (int i = 0; i <= array.length - 1; i++) {
            System.out.println("i:" + i);
            System.out.println(array[i]);
        }
        System.out.println("--------------");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + "\t");
        }
        System.out.println();
        System.out.println("--------------");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.print(array[i] + "\t");
        }
    }

    @Test
    public void test4() {
        //数组下标越界异常
        //Array Index OutOf Bounds Exception
        int[] array = {23, 34, 56, 7};
        //System.out.println(array[-1]);
        for (int i = 0; i <= array.length; i++) {
            System.out.println(array[i]);//array[4]
        }
    }

    @Test
    public void test6() {
        //i=1  j=3
        //i=2  j=2
        //i=3  j=1
        int[] array = {40, 17, 21, 1};
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test88() {
        // char[] array = {'a' , 'b', 'c', 'b' , 'a'};
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String str = scanner.nextLine();// "abcba"
        System.out.println(str);
        char[] array = str.toCharArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}
